package com.aide.security.auth;

import java.io.Console;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import com.aide.security.encoding.XXTEA;

public class AideRead {

	public static final DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
	private static final char pk[] = { 'a', '8', 'i', '3', 'd', '2', 'e', '9' };

	public AideRead() {
	}

	public static boolean v(String key, String sn) {
		try {
			Date c = new Date();
			String str[] = read(key, sn).split("\\|");
			String mac = GetSNShell.getPhysicalAddress();
			String macs[] = mac.split(","); 
			String as[];
			int j = (as = macs).length;
			for (int i = 0; i < j; i++) {
				String m = as[i];
				if (!m.equals(str[0])) {
					continue;
				} else {
					return false;
				}
			}
			if (GetSNShell.getPhysicalAddress().equals(str[0])
					&& df.parse(str[1]).after(c))
				return true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;

	}

	public static String vi(String key, String sn) {
		try {
			String info = "";
			Date c = new Date();
			String str[] = read(key, sn).split("\\|");
			String mac = GetSNShell.getPhysicalAddress();
			String macs[] = mac.split(",");
			boolean flag = true;
			String as[];
			int j = (as = macs).length;
			for (int i = 0; i < j; i++) {
				String m = as[i];
				if (!m.equals(str[0]))
					continue;
				flag = false;
				break;
			}

			if (flag)
				info = (new StringBuilder(String.valueOf(info)))
						.append("|MACERROR,").append(str[0]).append(",")
						.append(mac).toString();
			if (!df.parse(str[1]).after(c))
				info = (new StringBuilder(String.valueOf(info)))
						.append("|DATEERROR,").append(str[1]).append(",")
						.append(df.format(c)).toString();
			return info;
		} catch (Exception e) {
			return (new StringBuilder("EXCEPTIONERROR,"))
					.append(e.getMessage()).toString();
		}
	}

	private static String read(String key, String sn) throws Exception {
		String info = XXTEA.decrypt(sn, new String(pk));
		String dests[] = info.split("\\|");
		String mac = XXTEA.decrypt(dests[0], key);
		String date = XXTEA.decrypt(dests[1], key);
		return (new StringBuilder(String.valueOf(mac))).append("|")
				.append(date).toString();
	}

	public static String mac(String key) {
		String mstr = GetSNShell.getPhysicalAddress();
		String macs[] = mstr.split(",");
		String mac = macs[0];
		return mac(key, mac);
	}

	public static String mac(String key, String mac) {
		try {
			mac = XXTEA.encrypt(mac, key);
			return mac;
		} catch (Exception e) {
			return (new StringBuilder("ERROR:")).append(key).append(":")
					.append(mac).toString();
		}
	}

	public static void main(String args[]) throws Exception {
		System.out.println(GetSNShell.getPhysicalAddress());
		Console cons = System.console();
		System.out.print("用户名：");
		String user = cons.readLine();
		System.out.print("序列号：");
		String sn = cons.readLine();
		System.out.println(read(user, sn));
		System.out.println(v(user, sn));
		System.out.println(vi(user, sn));
	}

}